/**   
 * Copyright 2011 COMERE Harold
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package etrl.resources;

import com.google.gwt.user.client.Window;

/**
 * \class AResource
 * \brief Abstract class representing a random resource
 * @author harold
 *
 */
public abstract class AResource
{
	/**
	 * Flag determining if the resource is loaded or not
	 */
	private boolean _loaded;
	
	/**
	 * Flag determining if an error occured while resource loading
	 */
	private boolean _error;

	/**
	 * File path of the resource
	 */
	private String _resourceFilePath;
	
	/**
	 * \brief Initializes the resource.
	 * @param[in] filePath file path of the resource
	 */
	protected AResource(String filePath)
	{
		_resourceFilePath = filePath;
		
		// Initially the resource is not loaded .. and there is no error
		_loaded = false;
		_error = false;
	}
	
	/**
	 * \brief Sets the resource as loaded
	 * @param[in] success if true the loading ended without error, else an error happened
	 */
	protected void iAmLoaded(boolean success)
	{		
		_loaded = true;
		_error = ! success;
	}
	
	/**
	 * \brief Gets the resource file path
	 * @return resource file path
	 */
	protected String getFilePath()
	{
		return _resourceFilePath;
	}
	
	/**
	 * \brief Gets the load status
	 * @return true if the resource is loaded, else false
	 */
	public boolean isLoaded()
	{
		return _loaded;
	}
	
	/**
	 * \brief Gets the loading success status
	 * @return true if no error occured while loading, else false
	 */
	public boolean loadingIsSuccess()
	{
		return ! _error;
	}
	
	/**
	 * \brief Releases the resource
	 */
	public void release()
	{
		// Release data if needed
		onRelease();
		
		// And consider that the resource isnt loaded anymore
		_loaded = false;
	}
	
	/**
	 * \brief Launches the resource loading
	 * Each kind of resource shall implement it and set its boolean
	 * _loaded to true when its done
	 */
	public abstract void load();
	
	/**
	 * \brief Clears resource data at release
	 */
	public abstract void onRelease();	
	
}

